﻿// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

// ReSharper disable CheckNamespace
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable CommentTypo
// ReSharper disable IdentifierTypo
// ReSharper disable InconsistentNaming
// ReSharper disable StringLiteralTypo
// ReSharper disable UnusedParameter.Local

/*
 * Ars Magna project, http://arsmagna.ru
 */

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using AM.Reporting.Utils;

#endregion

#nullable enable

namespace AM.Reporting.Export.Html
{
    /// <summary>
    /// Represents the HTML export templates.
    /// </summary>
    public class HtmlTemplates
    {
        #region private fields

        private FastString capacitor;

        #endregion

        #region private methods

        private void NewCapacitor()
        {
            capacitor = new FastString (512);
        }

        private void Part (string str)
        {
            capacitor.AppendLine (str);
        }

        private string Capacitor()
        {
            return capacitor.ToString();
        }

        #endregion

        #region public properties

        /// <summary>
        /// Page Template Title
        /// </summary>
        public string PageTemplateTitle { get; set; }

        /// <summary>
        /// Page Template Footer
        /// </summary>
        public string PageTemplateFooter { get; set; }

        /// <summary>
        /// Navigator Template
        /// </summary>
        public string NavigatorTemplate { get; set; }

        /// <summary>
        /// OutlineTemplate
        /// </summary>
        public string OutlineTemplate { get; set; }

        /// <summary>
        /// Index Template
        /// </summary>
        public string IndexTemplate { get; set; }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlTemplates"/> class.
        /// </summary>
        public HtmlTemplates()
        {
            #region fill page template

            // {0} - title
            NewCapacitor();
            Part ("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            Part ("<html><head>");
            Part ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
            Part ("<meta name=Generator content=\"AM.Reporting http://www.fast-report.com\">");
            Part ("<title>{0}</title>");
            PageTemplateTitle = Capacitor();

            NewCapacitor();
            Part ("</html>");
            PageTemplateFooter = Capacitor();

            #endregion

            #region fill navigator template

            //  {0} - pages count {1} - name of report {2} multipage document {3} prefix of pages
            //  {4} first caption {5} previous caption {6} next caption {7} last caption
            //  {8} total caption
            NewCapacitor();
            Part ("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
            Part ("<html><head>");
            Part ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
            Part ("<meta name=Generator content=\"AM.Reporting http://www.fast-report.com\">");
            Part ("<title></title><style type=\"text/css\"><!--");
            Part (
                "body,input,select {{ font-family:\"Lucida Grande\",Calibri,Arial,sans-serif; font-size: 8px; font-weight: bold; font-style: normal; text-align: center; vertical-align: middle; }}");
            Part ("input {{text-align: center}}");
            Part (".nav {{ font-size : 9pt; color : #283e66; font-weight : bold; text-decoration : none;}}");
            Part ("--></style><script language=\"javascript\" type=\"text/javascript\"><!--");
            Part (
                "  var frCurPage = 1; var frPgCnt = {0}; var frRepName = \"{1}\"; var frMultipage = {2}; var frPrefix=\"{3}\";");
            Part ("  function DoPage(PgN) {{");
            Part ("    if ((PgN > 0) && (PgN <= frPgCnt) && (PgN != frCurPage)) {{");
            Part ("      if (frMultipage > 0)  parent.mainFrame.location = frPrefix + PgN + \".html\";");
            Part ("      else parent.mainFrame.location = frPrefix + \".main.html#PageN\" + PgN;");
            Part ("      UpdateNav(PgN); }} else document.PgForm.PgEdit.value = frCurPage; }}");
            Part ("  function UpdateNav(PgN) {{");
            Part ("    frCurPage = PgN; document.PgForm.PgEdit.value = PgN;");
            Part ("    if (PgN == 1) {{ document.PgForm.bFirst.disabled = 1; document.PgForm.bPrev.disabled = 1; }}");
            Part ("    else {{ document.PgForm.bFirst.disabled = 0; document.PgForm.bPrev.disabled = 0; }}");
            Part (
                "    if (PgN == frPgCnt) {{ document.PgForm.bNext.disabled = 1; document.PgForm.bLast.disabled = 1; }}");
            Part ("    else {{ document.PgForm.bNext.disabled = 0; document.PgForm.bLast.disabled = 0; }} }}");
            Part ("--></script></head>");
            Part (
                "<body bgcolor=\"#DDDDDD\" text=\"#000000\" leftmargin=\"0\" topmargin=\"4\" onload=\"UpdateNav(frCurPage)\">");
            Part (
                "<form name=\"PgForm\" onsubmit=\"DoPage(document.forms[0].PgEdit.value); return false;\" action=\"\">");
            Part ("<table cellspacing=\"0\" align=\"left\" cellpadding=\"0\" border=\"0\" width=\"100%\">");
            Part ("<tr valign=\"middle\">");
            Part (
                "<td width=\"60\" align=\"center\"><button name=\"bFirst\" class=\"nav\" type=\"button\" onclick=\"DoPage(1); return false;\"><b>{4}</b></button></td>");
            Part (
                "<td width=\"60\" align=\"center\"><button name=\"bPrev\" class=\"nav\" type=\"button\" onclick=\"DoPage(Math.max(frCurPage - 1, 1)); return false;\"><b>{5}</b></button></td>");
            Part (
                "<td width=\"100\" align=\"center\"><input type=\"text\" class=\"nav\" name=\"PgEdit\" value=\"frCurPage\" size=\"4\"></td>");
            Part (
                "<td width=\"60\" align=\"center\"><button name=\"bNext\" class=\"nav\" type=\"button\" onclick=\"DoPage(frCurPage + 1); return false;\"><b>{6}</b></button></td>");
            Part (
                "<td width=\"60\" align=\"center\"><button name=\"bLast\" class=\"nav\" type=\"button\" onclick=\"DoPage(frPgCnt); return false;\"><b>{7}</b></button></td>");
            Part ("<td width=\"20\">&nbsp;</td>\r\n");
            Part (
                "<td align=\"right\">{8}: <script language=\"javascript\" type=\"text/javascript\"> document.write(frPgCnt);</script></td>");
            Part ("<td width=\"10\">&nbsp;</td>");
            Part ("</tr></table></form></body></html>");
            NavigatorTemplate = Capacitor();

            #endregion

            #region fill outline template

            // under construction
            OutlineTemplate = string.Empty;

            #endregion

            #region fill index template

            // {0} - title, {1} - navigator frame, {2} - main frame
            NewCapacitor();
            Part (
                "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\"");
            Part ("<html><head>");
            Part ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
            Part ("<meta name=Generator content=\"AM.Reporting http://www.fast-report.com\">");
            Part ("<title>{0}</title>");
            Part ("<frameset rows=\"36,*\" cols=\"*\">");
            Part ("<frame name=\"topFrame\" src=\"{1}\" noresize frameborder=\"0\" scrolling=\"no\">");
            Part ("<frame name=\"mainFrame\" src=\"{2}\" frameborder=\"0\">");
            Part ("</frameset>");
            Part ("</html>");
            IndexTemplate = Capacitor();

            #endregion
        }
    }
}
